package com.hengtiansoft.lfy.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.chart.DemoHXSDKHelper;
import com.hengtiansoft.lfy.chart.DemoHXSDKModel;
import com.hengtiansoft.lfy.chart.applib.controller.HXSDKHelper;
import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.utils.Config;
import com.hengtiansoft.lfy.utils.DataCleanManager;
import com.hyphenate.EMCallBack;

/**
 * 设置页面
 */
public class LfySettingActivity extends BaseActivity
        implements OnClickListener {
    /**
     * 设置扬声器布局
     */
    private RelativeLayout mRlSwitchSpeaker;

    /**
     * 打开扬声器播放语音
     */
    private ImageView mIvSwitchOpenSpeaker;
    /**
     * 关闭扬声器播放语音
     */
    private ImageView mIvSwitchCloseSpeaker;
    private RelativeLayout mRlSwitchAutoplay;
    private ImageView mIvSwitchOpenAutoplay;
    private ImageView mIvSwitchCloseAutoplay;
    /**
     * 女声
     */
    private LinearLayout mLlSettingGirlChoose;
    /**
     * 男声
     */
    private LinearLayout mLlSettingBoyChoose;
    private LinearLayout mLlSettingGirlChooseIv;
    private LinearLayout mLlSettingGirlUnChooseIv;
    private LinearLayout mLlSettingBoyChooseIv;
    private LinearLayout mLlSettingBoyUnChooseIv;
    private RadioGroup mRadioGroupLfySettingSpeed;
    private RadioButton mRbtnLfySettingSpeedFast;
    private RadioButton mRbtnLfySettingSpeedMedium;
    private RadioButton mRbtnLfySettingSpeedSlow;
    /**
     * 打开新消息通知imageView
     */
    private ImageView mIvSwitchOpenNotification;
    /**
     * 关闭新消息通知imageview
     */
    private ImageView mIvSwitchCloseNotification;
    /**
     * 设置新消息通知布局
     */
    private RelativeLayout mRlSwitchNotification;
    /**
     * 设置声音布局
     */
    private RelativeLayout mRlSwitchSound;
    /**
     * 设置震动布局
     */
    private RelativeLayout mRlSwitchVibrate;

    private ImageView mIvBack;
    private Context mContext;
    private Button btn_loggedout;
    /**
     * 打开声音提示imageview
     */
    private ImageView mIvSwitchOpenSound;
    /**
     * 关闭声音提示imageview
     */
    private ImageView mIvSwitchCloseSound;
    /**
     * 声音和震动中间的那条线
     */
    private TextView mTvLineOne, mTvLineTwo;
    /**
     * 打开消息震动提示
     */
    private ImageView mIvSwitchOpenVibrate;
    /**
     * 关闭消息震动提示
     */
    private ImageView mIvSwitchCloseVibrate;
    DemoHXSDKModel model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_lfy_setting);
        initView();
        setListener();
    }

    private void initView() {
        mIvSwitchOpenVibrate = (ImageView) findViewById(
                R.id.iv_switch_open_vibrate);
        mIvSwitchCloseVibrate = (ImageView) findViewById(
                R.id.iv_switch_close_vibrate);
        mIvSwitchOpenSound = (ImageView) findViewById(
                R.id.iv_switch_open_sound);
        mIvSwitchCloseSound = (ImageView) findViewById(
                R.id.iv_switch_close_sound);
        mRlSwitchNotification = (RelativeLayout) findViewById(
                R.id.rl_switch_notification);
        mRlSwitchSound = (RelativeLayout) findViewById(R.id.rl_switch_sound);
        mRlSwitchVibrate = (RelativeLayout) findViewById(
                R.id.rl_switch_vibrate);
        mRlSwitchSpeaker = (RelativeLayout) findViewById(
                R.id.rl_switch_speaker);
        mIvSwitchOpenSpeaker = (ImageView) findViewById(
                R.id.iv_switch_open_speaker);
        mIvSwitchCloseSpeaker = (ImageView) findViewById(
                R.id.iv_switch_close_speaker);
        mIvSwitchOpenNotification = (ImageView) findViewById(
                R.id.iv_switch_open_notification);
        mTvLineOne = (TextView) findViewById(R.id.textview1);
        mTvLineTwo = (TextView) findViewById(R.id.textview2);
        mIvSwitchCloseNotification = (ImageView) findViewById(
                R.id.iv_switch_close_notification);
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mLlSettingGirlChoose = (LinearLayout) findViewById(
                R.id.ll_setting_girl_choose);
        mLlSettingBoyChoose = (LinearLayout) findViewById(
                R.id.ll_setting_boy_choose);
        mLlSettingGirlChooseIv = (LinearLayout) findViewById(
                R.id.ll_setting_girl_choose_iv);
        mLlSettingGirlUnChooseIv = (LinearLayout) findViewById(
                R.id.ll_setting_girl_unchoose_iv);
        mLlSettingBoyChooseIv = (LinearLayout) findViewById(
                R.id.ll_setting_boy_choose_iv);
        mLlSettingBoyUnChooseIv = (LinearLayout) findViewById(
                R.id.ll_setting_boy_unchoose_iv);
        mRadioGroupLfySettingSpeed = (RadioGroup) findViewById(
                R.id.rg_lfy_setting_speed);
        mRbtnLfySettingSpeedFast = (RadioButton) findViewById(
                R.id.rbtn_lfy_setting_speed_fast);
        mRbtnLfySettingSpeedMedium = (RadioButton) findViewById(
                R.id.rbtn_lfy_setting_speed_medium);
        mRbtnLfySettingSpeedSlow = (RadioButton) findViewById(
                R.id.rbtn_lfy_setting_speed_slow);
        mRlSwitchAutoplay = (RelativeLayout) findViewById(
                R.id.rl_switch_autoplay);
        mIvSwitchOpenAutoplay = (ImageView) findViewById(
                R.id.iv_switch_open_autoplay);
        mIvSwitchCloseAutoplay = (ImageView) findViewById(
                R.id.iv_switch_close_autoplay);
        mRadioGroupLfySettingSpeed.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                                                 int checkedId) {
                        // TODO Auto-generated method stub
                        if (mRbtnLfySettingSpeedFast.getId() == checkedId) {
                            LFYApplication.getInstance()
                                    .setSettingSpeedChoose(1.3f);
                        } else if (mRbtnLfySettingSpeedSlow
                                .getId() == checkedId) {
                            LFYApplication.getInstance()
                                    .setSettingSpeedChoose(0.7f);
                        } else {
                            LFYApplication.getInstance()
                                    .setSettingSpeedChoose(1f);
                        }
                    }
                });
        if (LFYApplication.getInstance().getSettingSexChoose().equals("m")) {
            mLlSettingGirlChooseIv.setVisibility(View.GONE);
            mLlSettingGirlUnChooseIv.setVisibility(View.VISIBLE);
            mLlSettingBoyChooseIv.setVisibility(View.VISIBLE);
            mLlSettingBoyUnChooseIv.setVisibility(View.GONE);
        } else {
            mLlSettingGirlUnChooseIv.setVisibility(View.GONE);
            mLlSettingGirlChooseIv.setVisibility(View.VISIBLE);
            mLlSettingBoyChooseIv.setVisibility(View.GONE);
            mLlSettingBoyUnChooseIv.setVisibility(View.VISIBLE);
        }

        if (LFYApplication.getInstance().getSettingSpeedChoose() == 1.3f) {
            mRbtnLfySettingSpeedFast.setChecked(true);
        } else if (LFYApplication.getInstance()
                .getSettingSpeedChoose() == 0.7f) {
            mRbtnLfySettingSpeedSlow.setChecked(true);
        } else {
            mRbtnLfySettingSpeedMedium.setChecked(true);
        }
        if (LFYApplication.getInstance().getSettingAutoPlayer()) {
            mIvSwitchOpenAutoplay.setVisibility(View.VISIBLE);
            mIvSwitchCloseAutoplay.setVisibility(View.GONE);
        } else {
            mIvSwitchCloseAutoplay.setVisibility(View.VISIBLE);
            mIvSwitchOpenAutoplay.setVisibility(View.GONE);
        }

        // 是否打开扬声器
        // the speaker is switched on or not?
        if (LFYApplication.getInstance().getSettingMsgSpeaker()) {
            mIvSwitchOpenSpeaker.setVisibility(View.VISIBLE);
            mIvSwitchCloseSpeaker.setVisibility(View.GONE);
        } else {
            mIvSwitchOpenSpeaker.setVisibility(View.GONE);
            mIvSwitchCloseSpeaker.setVisibility(View.VISIBLE);
        }
        model = (DemoHXSDKModel) HXSDKHelper.getInstance().getModel();
        // 震动和声音总开关，来消息时，是否允许此开关打开
        // the vibrate and sound notification are allowed or not?
        if (model.getSettingMsgNotification()) {
            mIvSwitchOpenNotification.setVisibility(View.VISIBLE);
            mIvSwitchCloseNotification.setVisibility(View.GONE);
            mTvLineTwo.setVisibility(View.VISIBLE);
            mTvLineOne.setVisibility(View.VISIBLE);
            mRlSwitchSound.setVisibility(View.VISIBLE);
            mRlSwitchVibrate.setVisibility(View.VISIBLE);
        } else {
            mIvSwitchOpenNotification.setVisibility(View.GONE);
            mIvSwitchCloseNotification.setVisibility(View.VISIBLE);
            mRlSwitchSound.setVisibility(View.GONE);
            mRlSwitchVibrate.setVisibility(View.GONE);
            mTvLineTwo.setVisibility(View.GONE);
            mTvLineOne.setVisibility(View.GONE);
        }
        // 是否打开声音
        // sound notification is switched on or not?
        if (model.getSettingMsgSound()) {
            mIvSwitchOpenSound.setVisibility(View.VISIBLE);
            mIvSwitchCloseSound.setVisibility(View.GONE);
        } else {
            mIvSwitchOpenSound.setVisibility(View.GONE);
            mIvSwitchCloseSound.setVisibility(View.VISIBLE);
        }

        // 是否打开震动
        // vibrate notification is switched on or not?
        if (model.getSettingMsgVibrate()) {
            mIvSwitchOpenVibrate.setVisibility(View.VISIBLE);
            mIvSwitchCloseVibrate.setVisibility(View.GONE);
        } else {
            mIvSwitchOpenVibrate.setVisibility(View.GONE);
            mIvSwitchCloseVibrate.setVisibility(View.VISIBLE);
        }
        btn_loggedout = (Button) findViewById(R.id.btn_loggedout);

        btn_loggedout.setVisibility(Config.isUserLogined()?View.VISIBLE:View.GONE);

        btn_loggedout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(LfySettingActivity.this).setTitle(getString(R.string.prompt)).setMessage(getString(R.string.loginout_title))
                        .setPositiveButton(getString(R.string.ok),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        logout();
                                    }
                                })
                        .setNegativeButton(getString(R.string.cancel), null).show();
            }
        });
    }
    /**
     * 退出登录
     */
    void logout() {
        final ProgressDialog pd = new ProgressDialog(mContext,
                ProgressDialog.THEME_HOLO_LIGHT);
        String st = getResources().getString(R.string.Are_logged_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        DemoHXSDKHelper.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Config.setUserToken(null);
                        Config.setUserName(null);
                        HXPreferenceUtils.getInstance().removeCurrentUserInfo();
                        pd.dismiss();
                        // 重新显示登陆页面
                        finish();
                        startActivity(
                                new Intent(mContext, LoginActivityV2.class));

                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        pd.dismiss();
                        Toast.makeText(mContext, getString(R.string.loginout_fail), Toast.LENGTH_SHORT)
                                .show();
                    }
                });
            }
        });

    }
    private void setListener() {
        mRlSwitchNotification.setOnClickListener(this);
        mRlSwitchSound.setOnClickListener(this);
        mRlSwitchVibrate.setOnClickListener(this);
        mRlSwitchSpeaker.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mLlSettingGirlChoose.setOnClickListener(this);
        mLlSettingBoyChoose.setOnClickListener(this);
        mRlSwitchAutoplay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_switch_notification:
                if (mIvSwitchOpenNotification.getVisibility() == View.GONE
                        | mIvSwitchCloseNotification
                        .getVisibility() == View.VISIBLE) {
                    // 当前为关闭，点击打开
                    mRlSwitchSound.setVisibility(View.VISIBLE);
                    mRlSwitchVibrate.setVisibility(View.VISIBLE);
                    mIvSwitchOpenNotification.setVisibility(View.VISIBLE);
                    mIvSwitchCloseNotification.setVisibility(View.GONE);
                    mTvLineTwo.setVisibility(View.VISIBLE);
                    mTvLineOne.setVisibility(View.VISIBLE);
                    // chatOptions.setNotificationEnable(true);
                    //  EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel()
                            .setSettingMsgNotification(true);
                } else {
                    // 当前为关闭，点击关闭
                    mRlSwitchSound.setVisibility(View.GONE);
                    mRlSwitchVibrate.setVisibility(View.GONE);
                    mTvLineTwo.setVisibility(View.GONE);
                    mTvLineOne.setVisibility(View.GONE);
                    mIvSwitchOpenNotification.setVisibility(View.GONE);
                    mIvSwitchCloseNotification.setVisibility(View.VISIBLE);
                    //  chatOptions.setNotificationEnable(false);
                    //   EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel()
                            .setSettingMsgNotification(false);
                }
                break;
            case R.id.rl_switch_sound:
                if (mIvSwitchOpenSound.getVisibility() == View.GONE
                        | mIvSwitchCloseSound.getVisibility() == View.VISIBLE) {
                    mIvSwitchOpenSound.setVisibility(View.VISIBLE);
                    mIvSwitchCloseSound.setVisibility(View.GONE);
                    //   chatOptions.setNoticeBySound(true);
                    //    EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel().setSettingMsgSound(true);
                } else {
                    mIvSwitchOpenSound.setVisibility(View.GONE);
                    mIvSwitchCloseSound.setVisibility(View.VISIBLE);
                    //  chatOptions.setNoticeBySound(false);
                    //  EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel().setSettingMsgSound(false);
                }
                break;
            case R.id.rl_switch_vibrate:
                if (mIvSwitchOpenVibrate.getVisibility() == View.GONE
                        | mIvSwitchCloseNotification
                        .getVisibility() == View.VISIBLE) {
                    mIvSwitchOpenVibrate.setVisibility(View.VISIBLE);
                    mIvSwitchCloseVibrate.setVisibility(View.GONE);
                    //  chatOptions.setNoticedByVibrate(true);
                    //   EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel().setSettingMsgVibrate(true);
                } else {
                    mIvSwitchOpenVibrate.setVisibility(View.GONE);
                    mIvSwitchCloseVibrate.setVisibility(View.VISIBLE);
                    //   chatOptions.setNoticedByVibrate(false);
                    //    EMChatManager.getInstance().setChatOptions(chatOptions);
                    HXSDKHelper.getInstance().getModel()
                            .setSettingMsgVibrate(false);
                }
                break;
            case R.id.rl_switch_speaker:
                if (mIvSwitchOpenSpeaker.getVisibility() == View.GONE
                        | mIvSwitchCloseSpeaker.getVisibility() == View.VISIBLE) {
                    mIvSwitchOpenSpeaker.setVisibility(View.VISIBLE);
                    mIvSwitchCloseSpeaker.setVisibility(View.GONE);
                    LFYApplication.getInstance().setSettingMsgSpeaker(true);
                } else {
                    mIvSwitchOpenSpeaker.setVisibility(View.GONE);
                    mIvSwitchCloseSpeaker.setVisibility(View.VISIBLE);
                    LFYApplication.getInstance().setSettingMsgSpeaker(false);
                }
                break;
            case R.id.rl_switch_autoplay:
                if (mIvSwitchOpenAutoplay.getVisibility() == View.GONE
                        | mIvSwitchCloseAutoplay.getVisibility() == View.VISIBLE) {
                    mIvSwitchOpenAutoplay.setVisibility(View.VISIBLE);
                    mIvSwitchCloseAutoplay.setVisibility(View.GONE);
                    LFYApplication.getInstance().setSettingAutoPlayer(true);
                } else {
                    mIvSwitchOpenAutoplay.setVisibility(View.GONE);
                    mIvSwitchCloseAutoplay.setVisibility(View.VISIBLE);
                    LFYApplication.getInstance().setSettingAutoPlayer(false);
                }
                break;

            case R.id.ll_setting_girl_choose:
                mLlSettingGirlUnChooseIv.setVisibility(View.GONE);
                mLlSettingGirlChooseIv.setVisibility(View.VISIBLE);
                mLlSettingBoyChooseIv.setVisibility(View.GONE);
                mLlSettingBoyUnChooseIv.setVisibility(View.VISIBLE);
                LFYApplication.getInstance().setSettingSexChoose("f");
                break;
            case R.id.ll_setting_boy_choose:
                mLlSettingGirlChooseIv.setVisibility(View.GONE);
                mLlSettingGirlUnChooseIv.setVisibility(View.VISIBLE);
                mLlSettingBoyChooseIv.setVisibility(View.VISIBLE);
                mLlSettingBoyUnChooseIv.setVisibility(View.GONE);
                LFYApplication.getInstance().setSettingSexChoose("m");
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

}
